﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using TagThisOrgProject.Models;

namespace TagThisOrgProject.Repository
{
    public class UserBookmarkRepository
    {
        public TagContext Context { get; set; }
        public BookmarkRepository BookmarkRepository { get; set; }
        public UserRepository UserRepositiry { get; set; }

        public UserBookmarkRepository(TagContext context)
        {
            this.Context = context;
        }

        /// <summary>
        /// Добавляет пользователю закладку, 
        /// </summary>
        /// <param name="userName"></param>
        /// <param name="bookmark"></param>
        /// <param name="isForSave"></param>
        /// <returns></returns>
        internal UserBookmark AddUserBookmark(string userName, Bookmark bookmark, bool isForSave = true)
        {
            if (this.UserRepositiry == null)
                this.UserRepositiry = new UserRepository(this.Context);
            var user = this.UserRepositiry.GetUser(userName);
            if (user == null)
                throw new Exception("Пользователь не найден. Код 0006");

            return AddUserBookmark(user, bookmark, isForSave);
        }

        internal UserBookmark AddUserBookmark(UserProfile user, Bookmark bookmark, bool isForSave = true)
        {
            var userBookmark = GetUserBookmark(user, bookmark);
            if (userBookmark != null)
                return userBookmark;

            userBookmark = Context.UserBookmarks.Add(new UserBookmark { UserId = user.UserId, BookmarkId = bookmark.BookmarkId });
            try
            {
                if (isForSave)
                    Context.SaveChanges();
            }
            catch
            {
                throw new Exception("Не удалось сохранить закладку пользователя. Код 0007");
            }
            return userBookmark;
        }
        internal UserBookmark GetUserBookmark(UserProfile u, Bookmark b)
        {
            return (from ub in Context.UserBookmarks
                    where ub.UserId == u.UserId && b.BookmarkId == ub.BookmarkId
                    select ub).FirstOrDefault();
        }
        internal UserBookmark GetUserBookMark(Int64 id)
        {
            return (from ub in Context.UserBookmarks
                    where ub.UserBookmarkId == id
                    select ub).FirstOrDefault();
        }
        internal IEnumerable<UserBookmark> GetUserBookmarks(UserProfile u, params Bookmark[] bookmarks)
        {
            var bookmarksId = bookmarks.Select(b => b.BookmarkId);
            return (from ub in Context.UserBookmarks
                    where ub.UserId == u.UserId && bookmarksId.Contains(ub.UserBookmarkId)
                    select ub);
        }
        internal IEnumerable<UserBookmark> AddUserBookmarks(string userName, params Bookmark[] bookmarks)
        {
            if (this.UserRepositiry == null)
                this.UserRepositiry = new UserRepository(this.Context);
            var user = this.UserRepositiry.GetUser(userName);
            if (user == null)
                throw new Exception("Пользователь не найден. Код 0006");
            var userBookmarks = new List<UserBookmark>();
            foreach (var b in bookmarks)
            {
                userBookmarks.Add(AddUserBookmark(user, b, false));
            }
            try
            {
                Context.SaveChanges();
            }
            catch
            {
                throw new Exception("Не удалось сохранить закладки пользователя. Код 0008");
            }
            return userBookmarks;
        }
    }
}